package com.gongpingjialoan.activity.car;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.fb.MyPushIntentService;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.activity.main.LoginActivity;
import com.gongpingjialoan.activity.main.MainActivity;
import com.gongpingjialoan.activity.sell.SellCarDelarsFragment;
import com.gongpingjialoan.activity.sell.SellCarPrivateFragment;
import com.gongpingjialoan.bean.Monitor;
import com.gongpingjialoan.global.GPJApplication;
import com.gongpingjialoan.utility.StepMonitor;
import com.gongpingjialoan.utility.StepRecord;
import com.gongpingjialoan.widget.LoadingDialog;

/**
 * Created by Administrator on 2016/5/19.
 */
public class SellCarHistoryActivity extends BaseActivity {

    public LoadingDialog loadingDialog;

    private static final int LOGINING_FORSEECAR = 4;

    private static final int REQUEST_REFRESH = 14;

    private String fromType = "";

    private ViewPager mViewPager;

    private RadioGroup mRadioGroup;

    private RadioButton mRadioButton, mRadioButton1;

    private SellCarPrivateFragment mSellCarPrivateFragment;

    private SellCarDelarsFragment mSellCarDelarsFragment;

    private MyViewPageAdapter mMyViewPageAdapter;

    private TextView deleteTextView;

    private boolean isPrivate = false;

    private int current = 0;

    private String[] actions = {"my_sellRecord_plat", "my_sellRecord_personal"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.sell_car_layout);
        isPrivate = getIntent().getBooleanExtra("private", false);
        deleteTextView = (TextView) findViewById(R.id.right);
        deleteTextView.setText("编辑");
        deleteTextView.setTextColor(0xff333333);
        deleteTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mViewPager.getCurrentItem() == 0) {
                    mSellCarDelarsFragment.showEdit(deleteTextView);
                } else if (mViewPager.getCurrentItem() == 1) {
                    mSellCarPrivateFragment.showEdit(deleteTextView);
                }
            }
        });


        mSellCarPrivateFragment = new SellCarPrivateFragment();
        mSellCarDelarsFragment = new SellCarDelarsFragment();
        setTitle("卖车记录");
        mViewPager = (ViewPager) findViewById(R.id.viewpage);
        mRadioGroup = (RadioGroup) findViewById(R.id.title_tab);
        mRadioButton = (RadioButton) findViewById(R.id.radio_new);
        mRadioButton1 = (RadioButton) findViewById(R.id.radio_experts);
        mMyViewPageAdapter = new MyViewPageAdapter(getSupportFragmentManager());
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.radio_new) {
                    if (mViewPager.getCurrentItem() != 0) {
                        current = 0;
                        mViewPager.setCurrentItem(0);
                    }
                } else {
                    if (mViewPager.getCurrentItem() != 1) {
                        current = 1;
                        mViewPager.setCurrentItem(1);
                    }
                }
            }
        });
        loadingDialog = new LoadingDialog(this);
        fromType = getIntent().getStringExtra("type");
        String source = getIntent().getStringExtra("source");
        String job_id = getIntent().getStringExtra("job_id");
        if (MyPushIntentService.SELLCARHISTORYDETAIL.equals(fromType) || MyPushIntentService.PERSONERSEllRECONDS.equals(fromType)) {
            StepRecord.recordStep(this, "pushnotification", "{type:" + from + ",typevalue:" + "" + ",source:" + source + ",job_id:" + job_id + "}");
        }

        getDataFromNet();

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                current = position;
                StepMonitor.getInstance().addMonitor(new Monitor("enter", actions[current], System.currentTimeMillis(), 1f));
                if (position == 0) {
                    mRadioButton.setChecked(true);
                    mSellCarPrivateFragment.cancle(deleteTextView);
                    if (mSellCarDelarsFragment.mAssessHistories.size() > 0) {
                        setEditVis(true, 0);
                    } else {
                        setEditVis(false, 0);
                    }
                } else {
                    mRadioButton1.setChecked(true);
                    mSellCarDelarsFragment.cancle(deleteTextView);
                    if (mSellCarPrivateFragment.mSellPrivateBeans.size() > 0) {
                        setEditVis(true, 1);
                    } else {
                        setEditVis(false, 1);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    public void setEditVis(boolean isVis, int current) {
        if (this.current == current) {
            if (isVis) {
                deleteTextView.setVisibility(View.VISIBLE);
            } else {
                deleteTextView.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        StepMonitor.getInstance().addMonitor(new Monitor("enter", actions[current], System.currentTimeMillis(), 1f));
    }


    private void getDataFromNet() {
        if (GPJApplication.getInstance().isLogin()) {
            mViewPager.setAdapter(mMyViewPageAdapter);
            if (isPrivate) {
                mRadioButton1.setChecked(true);
                current = 1;
            }
        } else {
            Intent intent = new Intent(this, LoginActivity.class);
            startActivityForResult(intent, LOGINING_FORSEECAR);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == LOGINING_FORSEECAR) {
                mViewPager.setAdapter(mMyViewPageAdapter);
                if (isPrivate) {
                    mRadioButton1.setChecked(true);
                    current = 1;
                }
            } else if (requestCode == REQUEST_REFRESH) {
                mSellCarPrivateFragment.refreshData();
            }
        } else {
            if (requestCode == LOGINING_FORSEECAR) {
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        }
    }




    @Override
    public void onBackPressed() {
        //表示从推送而来
        if (MyPushIntentService.SELLCARHISTORY.equals(fromType) || MyPushIntentService.PERSONERSEllRECONDS.equals(fromType)) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void initTitleBar() {
        View backV = findViewById(R.id.title_back);
        if (backV != null) {
            backV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    onBackPressed();
                }
            });
        }
    }

    private class MyViewPageAdapter extends FragmentPagerAdapter {

        public MyViewPageAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                return mSellCarDelarsFragment;
            } else {
                return mSellCarPrivateFragment;
            }
        }

        @Override
        public int getCount() {
            return 2;
        }
    }
}
